/**
 * 
 */
package org.cuatrovientos.car;


import java.util.Random;
import java.util.Vector;

/**
 * The class of race
 * @author sam_e_000
 *
 */

public class Race {

    
    private String name;
    private Circuit myCircuit;
    private Vector<Car> people = new Vector<Car>();
    
    
    /**
     * Constructor
     * @param name
     * @param myCircuit
     */
    public Race(String name, Circuit myCircuit) {
            this.name = name;
            this.myCircuit = myCircuit;
    }
    
    /**
     * @return the name
     */
    public String getName() {
            return name;
    }

    /**
     * Insert vehicles
     * @param v
     */
    public void insertCar(Car v){
            
            people.add(v);
    }
    
    /**
     * Run vehicles ;
     * @return
     */
    public Car run(){
            
            int pole;
            int index=0;
            int pole2;
             for (Car d: people){
            
                  
             
             
            
             int tam=people.size();
            for (int i =0 ;i < tam;i++){
                    if (people.get(i).name==null){
                            tam--;
                            people.remove(i);
                    }
                    
            }
             pole=1000;
             for (int i=0;i< people.size();i++){
                    pole2=timer(people.get(i));
                     if (pole2<pole){
                            index=i;
                            pole=pole2;
                    }
             }
            
             return people.get(index);
             
    }
    /**
     * Calculate timer
     * @param d
     * @return
     */
    private int timer(Car d){
            int seconds =0;
            int kilometers=0;
            while (kilometers < myCircuit.getPositions()){
                    seconds++;
                    kilometers+=d.move();
            }
            return seconds;
    }
             
             
    
    
    
}
